{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7a45d0a2-4f0a-4599-8c70-1755b397da27",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "from sklearn import svm\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.metrics import classification_report"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "34c2a570-431f-4923-82a9-af0c5728fed2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 这里我们使用随机数据作为示例\n",
    "num_samples = 100\n",
    "face_feature_dim = 32\n",
    "voice_feature_dim = 16"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ff2f3be-2eac-4292-9e63-c83b4b463927",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 假设我们有一些人脸特征和语音特征\n",
    "# 随机生成人脸特征和语音特征\n",
    "face_features = torch.randn(num_samples, face_feature_dim)\n",
    "voice_features = torch.randn(num_samples, voice_feature_dim)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "48af77fc-3e81-4154-890c-eded8fcaa7ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 假设我们也有一些标签\n",
    "labels = torch.randint(0, 2, (num_samples,))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "228eec83-4b58-4554-8d4b-f81480e2a1ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 特征融合：将人脸特征和语音特征串联起来\n",
    "fused_features = torch.cat((face_features, voice_features), dim=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7d5eb572-106a-4fa4-b664-97e3a919c834",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将PyTorch张量转换为NumPy数组，以便与scikit-learn兼容\n",
    "fused_features_np = fused_features.numpy()\n",
    "labels_np = labels.numpy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9b2c5691-6b8d-43c8-ae01-e243f2fa00d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义SVM参数网格进行搜索\n",
    "parameters = {'C': [1, 10, 100], 'gamma': [1e-2, 1e-3, 1e-4]}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89f2b87b-10c0-47fe-a380-c60ad7d88b95",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建SVM分类器实例\n",
    "svc = svm.SVC(kernel='rbf')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "52e0ed93-b781-4366-a2fe-192af2122de1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用网格搜索找到最优参数\n",
    "clf = GridSearchCV(svc, parameters, cv=5)\n",
    "\n",
    "# 训练SVM分类器\n",
    "clf.fit(fused_features_np, labels_np)\n",
    "\n",
    "# 输出最优参数\n",
    "print('Best parameters found by grid search are:', clf.best_params_)\n",
    "\n",
    "# 使用最优参数的SVM模型进行预测\n",
    "predictions = clf.predict(fused_features_np)\n",
    "\n",
    "# 输出分类报告\n",
    "print(classification_report(labels_np, predictions))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
